<?php
namespace app\common\model;
/**
 * 氨基酸片段
 */
class AminoFragment{
	/**
	 * 上一级片段序号
	 */
	private $mParentIndex;
	/**
	 * 片段时间序列号
	 */
	private $mCodeIndex;
	/**
	 * 片段序号
	 */
	private $mIndex;
	/**
	 * 是否包含特殊包括标记
	 */
	private $mHasFlag=false;
	/**
	 * 标记名称
	 */
	private $mFlagName='noFlag';
	/**
	 * 标记信息
	 */
	private $mFlagData;
	/**
	 * 序列具体信息
	 */
	private $mDetail;
	/**
	 * 所在的链上，默认为0，主链
	 */
	private $mChain='0';
	/**
	 * 子片段内容
	 */
	private $mFragments=null;
	
	/**
	 * 相关联的前一片段编号
	 */
	private $mPreFragmentIndex='-1';
	/**
	 * 相关联的后一片段编号
	 */
	private $mNextFragmentIndex='-1';
	private $mHasNterm=false;
	private $mNterm;
	
	private $mHasCterm=false;
	private $mCterm;
	
	public function __set($name, $value){
		$this->$name = $value;
	}
	
	public function __get($name){
		return $this->$name;
	}
	
	public function __construct(){
		$this->mCodeIndex = time().rand(1,100);
	}
	
	/**
	 * fragment转array
	 */
    public function toArray(){
		$detail = $this->getDetail();
		return array(
		   'index'=>$this->mIndex,
		   'code_index'=>$this->mCodeIndex,
		   'has_flag'=>$this->mHasFlag,
		   'flag_name'=>$this->mFlagName,
		   'flag_data'=>$this->mFlagData,
		   'chain'=>$this->mChain,
		   'detail'=>$detail,
		   'hasNterm'=>$this->mHasNterm,
		   'nterm'=>$this->mNterm,
		   'hasCterm'=>$this->mHasCterm,
		   'cterm'=>$this->mCterm,
		   'pre_fragment_index'=>$this->mPreFragmentIndex,
		   'next_fragment_index'=>$this->mNextFragmentIndex
		);
    }

    private function getDetail(){
    	$detail = $this->mDetail;
    	if(!is_null($detail)) return $detail;
		
		$fragments = $this->mFragments;
		if(count($fragments)==0) return null;
		
		$detail = array();
		foreach($fragments as $fragment){
			if(!is_null($fragment->mDetail)){
				$detail = array_merge($detail, $fragment->mDetail);
			}else{
				$has_flag = $fragment->mHasFlag;
				if(!$has_flag) continue;
				$flag_data = $fragment->mFlagData;
				if($flag_data['flag']==1){// flag=1 标记需要作为氨基酸计算
					array_push($detail, $flag_data['single']);
				}
			}
		}
		
		return $detail;
    }
}
